package com.java2007.zhangyu.restaurant.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.java2007.zhangyu.restaurant.dao.TableDao;
import com.java2007.zhangyu.restaurant.dao.impl.TableDaoImpl;
import com.java2007.zhangyu.restaurant.pojo.DinnerTable;
import com.java2007.zhangyu.restaurant.service.TableService;
import com.java2007.zhangyu.restaurant.utils.JdbcUtils;
import org.apache.commons.dbutils.DbUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author zy
 * @Version
 * @date 2021/01/28
 */
public class TableServiceImpl implements TableService {

    private TableDao tableDao = new TableDaoImpl();

    @Override
    public int saveTable(DinnerTable dinnerTable) throws SQLException {
        try {
            //开启事务
            JdbcUtils.begin();
            tableDao.saveTable(dinnerTable);
            //提交事务
            JdbcUtils.commitAndClose();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务
            JdbcUtils.rollbackAndClose();
            return -1;
        }
        return 1;
    }

    @Override
    public int deleteByTableId(Integer dtId) throws Exception{
        try {
            //开启事务
            JdbcUtils.begin();
            tableDao.deleteByTableId(dtId);
            //提交事务
            JdbcUtils.commitAndClose();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务
            JdbcUtils.rollbackAndClose();
            return -1;
        }
        return 1;
    }

    /**
     * 修改餐桌状态
     * @param table
     * @return
     * @throws SQLException
     */
    @Override
    public int update(DinnerTable table) throws SQLException {
        try {
            //开启事务
            JdbcUtils.begin();
            tableDao.update(table);
            //提交事务
            JdbcUtils.commitAndClose();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务
            JdbcUtils.rollbackAndClose();
            return -1;
        }
        return 1;
    }

    @Override
    public DinnerTable queryByTableId(Integer dtId) throws SQLException {
        return tableDao.queryByTableId(dtId);
    }

    @Override
    public List<DinnerTable> queryTableName(String dtName) throws SQLException {
        //判断搜索文本框中的状态
        //判断搜索文本框中是否为空
        if(StringUtils.isEmpty(dtName)){
            dtName = "";
        } else {
            //trim() 方法用于删除字符串的头尾空白符（空白符包括：空格、制表符 tab、换行符等其他空白符等）
            dtName = dtName.trim();
        }

        //此时，搜索文本框中一定有值 值为 "" 或 桌名
        return tableDao.queryTableName("%" + dtName + "%");
    }

    @Override
    public List<DinnerTable> queryByStatus(Integer dtState) throws Exception {
        return tableDao.queryByStatus(dtState);
    }

    @Override
    public List<DinnerTable> queryAll() throws Exception {
        return tableDao.queryAll();
    }

    @Override
    public int checkTableName(String dtName) {
        int result=0;
        try {
            JdbcUtils.begin();
            DinnerTable table = tableDao.selectTableByDtName(dtName);
            if(table !=null){
                result=1;
            }
            JdbcUtils.commitAndClose();
        }catch (Exception e){
            JdbcUtils.rollbackAndClose();
            e.printStackTrace();
        }
        return result;
    }
}
